using PK.Broadcaster;
//using PK.InAppPurchase;
using PKGames.Audio;
using PKGames.MiniGames.VoxelGame.UI.GameUI.Scripts;
using PKGames.SceneManagement;
using System;
using System.Collections;
using UnityEngine;
using Utils;
using VoxelEngine.UI;

namespace PKGames.MiniGames.VoxelGame.Scripts.World
{
	public class UiController : MonoBehaviour
        //, IMessageSubscriber<ProcessPurchaseSuccessMessage>
	{
		public enum UiState
		{
			AllWindowsClosed,
			SettingsOpenAnimation,
			SettingsOpen,
			SettingsCloseAnimation,
			ResetOpenAnimation,
			ResetOpen,
			ResetCloseAnimation,
			IapCreativeOpenAnimation,
			IapCreativeForceOpenAnimation,
			IapCreativeOpen,
			IapCreativeCloseAnimation
		}

		private UiState _uiState;

		[SerializeField]
		private CanvasGroup _resetProgressCanvasGroup;

		[SerializeField]
		private CanvasGroup _settingsCanvasGroup;

		[SerializeField]
		private ResetProgressController _resetProgressController;

		[SerializeField]
		private SettingsController _settingsController;

		//[SerializeField]
		//private IapCreativeUiController _iapCreativeUiController;

		[SerializeField]
		private TrialButtonController _trialButtonController;

		[SerializeField]
		private GamePanelUiController _gamePanelUiController;

		[SerializeField]
		private MultiFunctionButtonsController _multiFunctionButtonsController;

		private const float FadeTime = 0.15f;

		private const float TrialTime = 300f;

		private Func<bool> _isTrialEnabled;

		private Action _registerTrialEnd;

		//private StoreProduct _storeProduct;

		private void Awake()
		{
			//MessageBroadcaster.Instance.Subscribe(this);
		}

		private void OnDestroy()
		{
			//MessageBroadcaster.Instance.Unsubscribe(this);
		}

		public void Setup(Func<bool> isTrial, Sprite iapSprite, Action onTrialEnd, object storeProduct, string levelName, BlockSelectionData blockSelectionData)
		{
			_resetProgressController.SetupProgressController(OnClose);
			_settingsController.Setup(OpenResetProgressPopup, LeaveGame);
			_multiFunctionButtonsController.Setup(ProcessLeftButtonPress, ProcessRightButtonPress);
			_trialButtonController.Setup(isTrial(), 300f, OnTrialButtonTouched, OnTimerFinish);
			//_iapCreativeUiController.Setup(iapSprite, storeProduct, levelName);
			_gamePanelUiController.Setup(blockSelectionData);
			_isTrialEnabled = isTrial;
			_registerTrialEnd = onTrialEnd;
			//_storeProduct = storeProduct;
		}

		private void OnClose()
		{
			TryChangingToNextState(UiState.ResetCloseAnimation);
		}

		private void OpenResetProgressPopup()
		{
			TryChangingToNextState(UiState.ResetOpenAnimation);
            AdManager.ShowNative();
        }

		private void LeaveGame()
		{
			MonoSingleton<AudioManager>.Instance.Play("back");
			MonoSingleton<SceneLoader>.Instance.GotoPreviousScene();
            AdManager.ShowNative();
        }

		private void OnTrialButtonTouched()
		{
			TryChangingToNextState(UiState.IapCreativeOpenAnimation);
		}

		private void OnTimerFinish()
		{
			_registerTrialEnd.Fire();
			TryChangingToNextState(UiState.IapCreativeForceOpenAnimation);
		}

		private void ProcessLeftButtonPress()
		{
			if (_uiState == UiState.AllWindowsClosed && !_isTrialEnabled())
			{
				TryChangingToNextState(UiState.SettingsOpenAnimation);
			}
			else if (_uiState == UiState.AllWindowsClosed && _isTrialEnabled())
			{
				TryChangingToNextState(UiState.IapCreativeOpenAnimation);
			}
			else if (_uiState == UiState.SettingsOpen)
			{
				TryChangingToNextState(UiState.SettingsCloseAnimation);
			}
			else if (_uiState == UiState.IapCreativeOpen)
			{
				TryChangingToNextState(UiState.IapCreativeCloseAnimation);
            }
            AdManager.ShowNative();
        }

		private void ProcessRightButtonPress()
		{
			if (_uiState == UiState.IapCreativeOpen)
			{
				if (_isTrialEnabled())
				{
					_registerTrialEnd.Fire();
				}
				LeaveGame();
			}
		}

		private void TryChangingToNextState(UiState nextState)
		{
			if (CanChangeToNextState(nextState))
			{
				ChangeToNextState(nextState);
			}
		}

		private bool CanChangeToNextState(UiState nextState)
		{
			switch (_uiState)
			{
			case UiState.AllWindowsClosed:
				return nextState == UiState.SettingsOpenAnimation || nextState == UiState.IapCreativeOpenAnimation || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.SettingsOpenAnimation:
				return nextState == UiState.SettingsOpen || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.SettingsOpen:
				return nextState == UiState.SettingsCloseAnimation || nextState == UiState.ResetOpenAnimation || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.SettingsCloseAnimation:
				return nextState == UiState.AllWindowsClosed || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.ResetOpenAnimation:
				return nextState == UiState.ResetOpen || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.ResetOpen:
				return nextState == UiState.ResetCloseAnimation || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.ResetCloseAnimation:
				return nextState == UiState.SettingsOpen || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.IapCreativeOpenAnimation:
				return nextState == UiState.IapCreativeOpen || nextState == UiState.IapCreativeCloseAnimation || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.IapCreativeForceOpenAnimation:
				return nextState == UiState.IapCreativeOpen || nextState == UiState.IapCreativeCloseAnimation;
			case UiState.IapCreativeOpen:
				return nextState == UiState.IapCreativeCloseAnimation || nextState == UiState.IapCreativeForceOpenAnimation;
			case UiState.IapCreativeCloseAnimation:
				return nextState == UiState.AllWindowsClosed || nextState == UiState.IapCreativeForceOpenAnimation;
			default:
				throw new ArgumentOutOfRangeException();
			}
		}

		private void ChangeToNextState(UiState nextState)
		{
			_uiState = nextState;
			UpdateAccordingToState(_uiState);
		}

		private void UpdateAccordingToState(UiState uiState)
		{
			_multiFunctionButtonsController.SetButtonSkinsAccordingToState(uiState);
			switch (uiState)
			{
			case UiState.IapCreativeOpen:
				break;
			case UiState.AllWindowsClosed:
				SetSettingsClosedState();
				break;
			case UiState.SettingsOpenAnimation:
				SetSettingsOpenAnimationState();
				break;
			case UiState.SettingsOpen:
				SetSettingsOpenState();
				break;
			case UiState.SettingsCloseAnimation:
				SetSettingsCloseAnimationState();
				break;
			case UiState.ResetOpenAnimation:
				SetResetOpenAnimationState();
				break;
			case UiState.ResetOpen:
				SetResetOpenState();
				break;
			case UiState.ResetCloseAnimation:
				SetResetCloseAnimationState();
				break;
			case UiState.IapCreativeOpenAnimation:
				SetIapCreativeOpenAnimationState();
				break;
			case UiState.IapCreativeForceOpenAnimation:
				SetIapCreativeForceOpenAnimationState();
				break;
			case UiState.IapCreativeCloseAnimation:
				SetIapCreativeCloseAnimationState();
				break;
			default:
				throw new ArgumentOutOfRangeException("uiState", uiState, null);
			}
		}

		private void SetSettingsClosedState()
		{
			_settingsCanvasGroup.alpha = 0f;
			_settingsCanvasGroup.interactable = false;
			_settingsCanvasGroup.blocksRaycasts = false;
		}

		private void SetSettingsOpenAnimationState()
		{
			MonoSingleton<AudioManager>.Instance.Play("select_simple");
			_settingsCanvasGroup.interactable = false;
			_settingsCanvasGroup.blocksRaycasts = true;
			StartCoroutine(_settingsCanvasGroup.Fade(0f, 1f, 0.15f, delegate
			{
				TryChangingToNextState(UiState.SettingsOpen);
			}));
		}

		private void SetSettingsOpenState()
		{
			_settingsCanvasGroup.alpha = 1f;
			_settingsCanvasGroup.interactable = true;
			_settingsCanvasGroup.blocksRaycasts = true;
			_resetProgressCanvasGroup.interactable = false;
			_resetProgressCanvasGroup.blocksRaycasts = false;
		}

		private void SetSettingsCloseAnimationState()
		{
			MonoSingleton<AudioManager>.Instance.Play("back");
			_settingsCanvasGroup.interactable = false;
			_settingsCanvasGroup.blocksRaycasts = true;
			StartCoroutine(_settingsCanvasGroup.Fade(1f, 0f, 0.15f, delegate
			{
				TryChangingToNextState(UiState.AllWindowsClosed);
			}));
		}

		private void SetResetOpenAnimationState()
		{
			MonoSingleton<AudioManager>.Instance.Play("select_simple");
			_settingsCanvasGroup.interactable = false;
			_settingsCanvasGroup.blocksRaycasts = false;
			_resetProgressCanvasGroup.interactable = false;
			_resetProgressCanvasGroup.blocksRaycasts = true;
			StartCoroutine(_resetProgressCanvasGroup.Fade(0f, 1f, 0.15f, delegate
			{
				TryChangingToNextState(UiState.ResetOpen);
			}));
		}

		private void SetResetOpenState()
		{
			_resetProgressCanvasGroup.interactable = true;
			_resetProgressCanvasGroup.blocksRaycasts = true;
		}

		private void SetResetCloseAnimationState()
		{
			MonoSingleton<AudioManager>.Instance.Play("back");
			_settingsCanvasGroup.interactable = false;
			_settingsCanvasGroup.blocksRaycasts = false;
			_resetProgressCanvasGroup.interactable = false;
			_resetProgressCanvasGroup.blocksRaycasts = true;
			StartCoroutine(_resetProgressCanvasGroup.Fade(1f, 0f, 0.15f, delegate
			{
				TryChangingToNextState(UiState.SettingsOpen);
			}));
		}

		private void SetIapCreativeOpenAnimationState()
		{
			//StartCoroutine(SetIapCreativeOpenAnimationStateAsync());
		}

		//private IEnumerator SetIapCreativeOpenAnimationStateAsync()
		//{
		//	yield return _iapCreativeUiController.ShowIapCreativePopup(IapCreativeUiController.IapTrigger.LevelTrial, 0.15f, OnCloseIap);
		//	TryChangingToNextState(UiState.IapCreativeOpen);
		//}

		private void OnCloseIap()
		{
			TryChangingToNextState(UiState.IapCreativeCloseAnimation);
		}

		private void SetIapCreativeForceOpenAnimationState()
		{
			//StartCoroutine(SetIapCreativeForceOpenAnimationStateAsync());
		}

		//private IEnumerator SetIapCreativeForceOpenAnimationStateAsync()
		//{
		//	yield return _iapCreativeUiController.ShowIapCreativePopup(IapCreativeUiController.IapTrigger.LevelTimerEnded, 0.15f, OnCloseIap);
		//	TryChangingToNextState(UiState.IapCreativeOpen);
		//}

		private void SetIapCreativeCloseAnimationState()
		{
			//StartCoroutine(SetIapCreativeCloseAnimationStateAsync());
		}

		//private IEnumerator SetIapCreativeCloseAnimationStateAsync()
		//{
		//	yield return _iapCreativeUiController.Close(0.15f);
		//	TryChangingToNextState(UiState.AllWindowsClosed);
		//}

		//public void OnMessageReceived(ProcessPurchaseSuccessMessage message)
		//{
		//	if (message.StoreProduct.StoreId == _storeProduct.StoreId)
		//	{
		//		TurnOffTrial();
		//	}
		//}

		private void TurnOffTrial()
		{
			_trialButtonController.StopCountdown();
			if (_uiState == UiState.IapCreativeOpen || _uiState == UiState.IapCreativeOpenAnimation || _uiState == UiState.IapCreativeForceOpenAnimation)
			{
				TryChangingToNextState(UiState.IapCreativeCloseAnimation);
			}
		}
	}
}
